package com.emm.yixun.common.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import com.alibaba.fastjson.JSONObject;

/**
 * 
 * @author <a href="mailto:just_szl@hotmail.com"> Geray</a>
 * @version 1.0,2012-6-12
 */
public class FileUploadPorxyUtil {
	//移动端上传
	public static String appPost(String url, String imgContent,String fileName) {
		byte[] base64Bytes = Base64.decodeBase64(imgContent);
		File saveSourceFile=byte2File(base64Bytes,fileName);
		String result = submitPost(url, null, saveSourceFile);
		if (saveSourceFile.exists() && saveSourceFile.isFile()) {
			saveSourceFile.delete();
		}
		return result;

	}
	

	private static File byte2File(byte[] buf, String fileName) {
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		File file = null;
		try {
			File dir = new File(fileName);
			if (!dir.exists()) {
				dir.createNewFile();
			}
			file = new File(fileName);
			fos = new FileOutputStream(file);
			bos = new BufferedOutputStream(fos);
			bos.write(buf);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}

	private static String submitPost(String url, JSONObject params, File _file) {
		HttpClient httpclient = new DefaultHttpClient();
		String result = null;
		JSONObject res = new JSONObject();
		try {
			HttpPost httppost = new HttpPost(url);
			FileBody file = new FileBody(_file);
			MultipartEntity reqEntity = new MultipartEntity();
			reqEntity.addPart("file", file);// file1为请求后台的File upload;属性
			if (params != null) {
				Iterator<?> it = params.keySet().iterator();
				while (it.hasNext()) {
					String key = (String) it.next();
					String value = params.getString(key);
					reqEntity.addPart(key, new StringBody(value));// filename1为请求后台的普通参数;属性
				}
			}
			httppost.setEntity(reqEntity);

			HttpResponse response = httpclient.execute(httppost);

			int statusCode = response.getStatusLine().getStatusCode();

			if (statusCode == HttpStatus.SC_OK) {

				System.out.println("服务器正常响应.....");

				HttpEntity resEntity = response.getEntity();
				result = EntityUtils.toString(resEntity);
				EntityUtils.consume(resEntity);
			} else {
				res.put("result", "0");
				res.put("errorMsg", "服务器错误！");
			}

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			res.put("result", "0");
			res.put("errorMsg", "文件转换错误！");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			res.put("result", "0");
			res.put("errorMsg", "文件读写错误！");
			e.printStackTrace();
		} finally {
			try {
				httpclient.getConnectionManager().shutdown();
			} catch (Exception ignore) {

			}
		}
		result = result != null ? result : res.toJSONString();
		return result;
	}

}
